<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/05/26 17:32:08 $)
// $RCSfile: functions_winapp.php,v $ : $Revision: 1.2 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

define("DATA_COMPRESSED", "compressed");
define("DATA_UNCOMPRESSED", "uncompressed");

/*
* Decodes the winapp data
* For Compressed Data:
* compressed;<MD5 HASH OF COMPRESSED DATA>;<MD5 HASH OF UNCOMPRESSED DATA>;<SIZE OF COMPRESSED DATA>;<SIZE OF UNCOMPRESSED DATA>;<ACTUAL COMPRESSED DATA>
* For Uncompressed Data:
* uncompressed;0;<MD5 HASH OF UNCOMPRESSED DATA>;0;<SIZE OF UNCOMPRESSED DATA>;<ACTUAL UNCOMPRESSED DATA>
*/
function decodeWinappData($_data)
{
	global $_SWIFT;

	$_odatatype = strpos($_data, ";");
	$datatype = substr($_data, 0, $_odatatype);

	$_omd5compress = strpos($_data, ";", ($_odatatype+1));
	$md5compress = substr($_data, ($_odatatype+1), ($_omd5compress-$_odatatype-1));

	$_omd5uncompress = strpos($_data, ";", ($_omd5compress+1));
	$md5uncompress = substr($_data, ($_omd5compress+1), ($_omd5uncompress-$_omd5compress-1));

	$_osizecompress = strpos($_data, ";", ($_omd5uncompress+1));
	$sizecompress = substr($_data, ($_omd5uncompress+1), ($_osizecompress-$_omd5uncompress-1));

	$_osizeuncompress = strpos($_data, ";", ($_osizecompress+1));
	$sizeuncompress = substr($_data, ($_osizecompress+1), ($_osizeuncompress-$_osizecompress-1));

	$_odata = strpos($_data, ";", ($_osizeuncompress+1));
	$data = substr($_data, ($_osizeuncompress+1), strlen($_data));

//	echo "Data Type: $datatype<BR />MD5 Compress: $md5compress<BR />MD5 Uncompress: $md5uncompress<BR />Size Compress: $sizecompress<BR />Size Uncompress: $sizeuncompress<BR />Data: $data";
	if ($datatype == DATA_UNCOMPRESSED)
	{
		return $data;
	} else if ($datatype == DATA_COMPRESSED && extension_loaded("zlib")) {
		if (SWIFT_AREA == SWIFT_SYNCWORKS)
		{
			$_SWIFT["_debugdata"] .= "Base64 Data: \n".base64_decode($data)."\n";
		}

		return gzuncompress(base64_decode($data));
	} else {
		return $_data;
	}
}
?>